package com.issoft.studentresitor.service.impl;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.issoft.studentresitor.domain.pojo.Major;
import com.issoft.studentresitor.domain.pojo.Result;
import com.issoft.studentresitor.domain.pojo.Student;
import com.issoft.studentresitor.domain.query.MajorPageQuery;
import com.issoft.studentresitor.domain.vo.MajorVo;
import com.issoft.studentresitor.domain.vo.ReportVo;
import com.issoft.studentresitor.mapper.MajorMapper;
import com.issoft.studentresitor.service.IMajorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.issoft.studentresitor.utils.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author GJH
 * @since 2025-09-14
 */
@Slf4j
@Service
public class MajorServiceImpl extends ServiceImpl<MajorMapper, Major> implements IMajorService {
    @Autowired
    private MajorMapper majorMapper;

    @Override
    public PageResult<MajorVo> queryMajorPage(MajorPageQuery param) {
        //分页查询
        log.info("分页查询学生信息");
        log.info("参数：{}", param);
        //设置分页参数
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<MajorVo> MajorVos=majorMapper.queryMajorPage(param);
        Page<MajorVo> pageResult =(Page<MajorVo>) MajorVos;
        return new PageResult<>(pageResult.getTotal(), pageResult.getResult());
    }

    @Override
    public Result export(HttpServletResponse response, List<Long> ids) throws IOException {
        //1.查询数据
        List<Major> majors = majorMapper.selectBatchIds(ids);
        log.info("导出数据：{}", majors);
        //通过工具类创建writer,默认创建xls格式
        ExcelWriter excelWriter = ExcelUtil.getWriter(true);
        excelWriter.write(majors, true);
        //设置浏览器响应格式导出文件名
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        String fileName = URLEncoder.encode("专业信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        //获取输出流
        ServletOutputStream outputStream = response.getOutputStream();
        excelWriter.flush(outputStream, true);
        excelWriter.close();
        outputStream.close();
        return Result.success();
    }

    @Override
    public List<ReportVo> queryMajorCount() {
        List<ReportVo> reportVos = majorMapper.queryMajorCount();
        log.info("查询各专业人数：{}", reportVos);
        return reportVos;
    }

    @Override
    public List<ReportVo> queryClazzCount() {
        return majorMapper.queryClazzCount();
    }

    @Override
    public List<ReportVo> queryDormCount() {
        return majorMapper.queryDormCount();
    }

    @Override
    public List<ReportVo> queryPayCount() {
        return majorMapper.queryPayCount();
    }
}
